package com.aijk.web.admin.utils;

import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import javax.servlet.ServletOutputStream;
import java.io.*;

/**
 * POI工具类
 */
public class POIUtil {

    public static void htmlToWord(String body, InputStream is,
                                  ServletOutputStream os) throws IOException {
        StringBuffer strb = new StringBuffer();
        strb.append("<html>")
                .append("    <head><meta charset=\"utf-8\"/></head>")
                .append("    <body>").append(body).append("</body>")
                .append("</html>");
        String html = strb.toString();
        is = new ByteArrayInputStream(html.getBytes("UTF-8"));
        POIFSFileSystem fs = new POIFSFileSystem();
        fs.createDocument(is, "WordDocument");
        fs.writeFilesystem(os);
    }

    /**
     * 把输入流里面的内容以UTF-8编码当文本取出。
     * 不考虑异常，直接抛出
     *
     * @param ises
     * @throws IOException
     */
    private String getContent(InputStream... ises) throws IOException {
        if (ises != null) {
            StringBuilder result = new StringBuilder();
            BufferedReader br;
            String line;
            for (InputStream is : ises) {
                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                while ((line = br.readLine()) != null) {
                    result.append(line);
                }
            }
            return result.toString();
        }
        return null;
    }

}
